package cn.tedu.medicalpath.patient.controller;

import cn.tedu.medicalpath.base.response.JsonResult;
import cn.tedu.medicalpath.base.response.StatusCode;
import cn.tedu.medicalpath.patient.pojo.param.DoctorQuery;
import cn.tedu.medicalpath.patient.pojo.param.PatientQuery;
import cn.tedu.medicalpath.patient.pojo.param.PatientSaveParam;
import cn.tedu.medicalpath.patient.pojo.vo.DoctorDepartmentVO;
import cn.tedu.medicalpath.patient.pojo.vo.DoctorVO;
import cn.tedu.medicalpath.patient.pojo.vo.PatientVO;
import cn.tedu.medicalpath.patient.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用于实现门诊业务的患者注册
 * registration表示登记、注册
 */
@Slf4j
@RestController
@RequestMapping("/v1/patient/registration")
@Api(tags = "门诊患者模块")
public class PatientController {
    @Autowired
    private PatientService patientService;

    /**
     * 根据前端传来的数据查询是否有患者信息
     * 使用PostMapping保护数据安全
     * 只返回必要的信息，同时遵守隐私保护法规
     */
    @ApiOperation("查询患者信息")
    @PostMapping("/selectPatient")
    public JsonResult selectPatient(PatientQuery patientQuery){
        if (patientQuery == null || patientQuery.getPersonId() == null){
            return new JsonResult(StatusCode.FAILED);
        }
        PatientVO patientVO = patientService.selectPatient(patientQuery);
        return JsonResult.success(patientVO);
    }
    @ApiOperation("保存患者信息")
    @PostMapping("/savePatient")
    public JsonResult savePatient(PatientSaveParam patientSaveParam){
        log.debug("Controller-保存患者信息-patientSaveParam:{}",patientSaveParam);
        //校验保存信息不能为空
        if (patientSaveParam == null || patientSaveParam.getPersonId() == null || patientSaveParam.getName() == null){
            return new JsonResult(StatusCode.FAILED);
        }
        //验证身份证号格式
        boolean isValidPersonId = isValidIdCard(patientSaveParam.getPersonId());
        if (!isValidPersonId){
            return new JsonResult(StatusCode.FAILED);
        }
        Integer result = patientService.savePatient(patientSaveParam);
        log.debug("Controller-保存患者信息成功:{}",result);
        return JsonResult.success(result);//1,成功，0，失败
    }
    @ApiOperation("查询科室信息")
    @GetMapping("/selectDepartment")
    public JsonResult selectDepartment(){
        log.debug("Controller-查询科室信息");
        List<DoctorDepartmentVO> departmentVOList = patientService.selectDepartment();
        log.debug("Controller-查询科室信息成功:{}",departmentVOList);
        return JsonResult.success(departmentVOList);
    }
    @ApiOperation("查询科室对应的医生信息")
    @GetMapping("/selectDoctor")
    public JsonResult selectDoctor(DoctorQuery doctorQuery){
        log.debug("Controller-查询科室对应的医生信息列表",doctorQuery);
        List<DoctorVO> doctorVOList = patientService.selectDoctor(doctorQuery);
        log.debug("Controller-查询科室对应的医生信息成功:{}",doctorVOList);
        return JsonResult.success(doctorVOList);
    }

    private static boolean isValidIdCard(String personId) {
        // 用于验证18位身份证号的正则表达式
        final String ID_CARD_REGEX_18 = "^[1-9]\\d{5}(18|19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}([0-9]|X)$";
/*        // 用于验证15位身份证号的正则表达式
        final String ID_CARD_REGEX_15 = "^[1-9]\\d{5}\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}$";*/
        if (personId.isEmpty()) {
            return false;
        }
        // 移除可能存在的空格
        personId = personId.trim().toUpperCase();
        // 检查身份证号长度和格式
        if (personId.length() == 18) {
            return personId.matches(ID_CARD_REGEX_18);
        } /*else if (personId.length() == 15) {
            return personId.matches(ID_CARD_REGEX_15);
        }*/ else {
            return false;
        }
    }


}
